package com.ATMgui;

import com.lovo.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AlterPassword extends JPanel implements ActionListener {
    private Icon sureBtnImg = Util.getIcon("/image/buttonSure.jpg");
    private JPasswordField jPasswordField;
    private JPasswordField jPasswordField1;
    private JButton sure;
    private ATM frame;


    public AlterPassword(ATM frame) {
        this.frame = frame;
        //旧密码

        this.setLayout(null);
        JLabel textlabel1 = new JLabel("旧密码：");
        textlabel1.setBounds(200, 150, 50, 30);
        this.add(textlabel1);

        this.setOpaque(false);
        jPasswordField = new JPasswordField();
        jPasswordField.setBounds(250, 150, 250, 30);
        this.add(jPasswordField);

        JLabel textlabel2 = new JLabel("新密码：");
        textlabel2.setBounds(200, 200, 50, 30);
        this.add(textlabel2);
        //新密码
        this.setOpaque(false);
        jPasswordField1 = new JPasswordField();
        jPasswordField1.setBounds(250, 200, 250, 30);
        this.add(jPasswordField1);


        this.setOpaque(false);
        sure = new JButton(sureBtnImg);
        sure.setBounds(200, 320, 100, 20);
        this.add(sure);
        sure.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sure) {
            if (!String.valueOf(jPasswordField.getPassword()).equals(frame.getUsersList().get(frame.getNum()).getPassWord())) {
                JOptionPane.showMessageDialog(this, "输入旧密码错误");
            } else if (frame.getUsersList().get(frame.getNum()).getPassWord().equals(String.valueOf(jPasswordField1.getPassword()))) {
                JOptionPane.showMessageDialog(this, "两次密码输入一致");
            } else {
                frame.switchPanel("登陆");
            }
            frame.getUsersList().get(frame.getNum()).setPassWord(String.valueOf(jPasswordField1.getPassword()));

        }
    }
}
